package com.ctrip.ec.biz.dao.activity;
import com.ctrip.ec.biz.entity.dict.DictEntity;
import com.ctrip.ec.dal.DaoBase;
import com.ctrip.ec.biz.entity.activity.ActivityEntity;
import com.ctrip.ec.dal.DbAccess;
import org.springframework.stereotype.Service;

import java.sql.SQLException;

@Service
public class ActivityDao extends DaoBase<ActivityEntity> {
    public ActivityDao() {
        super(ActivityEntity.class);
    }
    public ActivityDao(DbAccess db) {
        super(ActivityEntity.class,db);
    }

    public void changeCategory(DictEntity src, DictEntity dest) throws SQLException {
        String sql = "update " + this.getTableName() + " set category_id=? ,category_name=? where category_id=? and begin_time<now()";
        this.db.execute(sql,dest.getId(),dest.getName(),src.getId());
    }

    public boolean hasNew(Long categoryId) throws SQLException {
        String sql = "select count(*) from "+ this.getTableName() + "  where date_add(create_time, interval 1 day) > now() and category_id=?";
        Long count = this.db.getScalar(Long.class,sql,categoryId);
        return count>0;
    }

    public ActivityEntity latestByBrandId(Long brandId) throws SQLException {
        String sql = "select * from " + this.getTableName() + " where brand_id=? order by id desc limit 1";
        ActivityEntity entity = this.db.getScalar(ActivityEntity.class,sql,brandId);
        return entity;
    }
}
